<?php 

/**
 * 分类管理控制器
 * @author 阿柱 <422852954@qq.com>
 */
class CategoryController extends Controller{
    /**
     *   默认访问方法
     */
    public function index(){
    	$sql="SELECT * FROM hd_category";
		$category=M()->query($sql);
//		p($cate);
		$this->assign('category',$category);		
		
        $this->display('category.html');
    }
	
	/**
	 * 添加顶级分类
	 */
	
	public function addTopCate(){
		if(IS_POST){
			$title=$_POST['title'];
			$sql="INSERT INTO hd_category SET title={$title}";
			M()->exec($sql);
			$this->success('添加成功',U('index'));
		}
		$this->display('addTopCate.html');
	}
	
	/**
	 * 增加子分类
	 */
	public function addSonCate(){
			
		if(IS_POST){
			$pid=$_POST['pid'];
			$title=$_POST['title'];
			$sql="INSERT INTO hd_category SET title='{$title}',pid={$pid}";
			M()->exec($sql);
			$this->success('添加成功',U('index'));
		}
		
		$this->display('addSonCate.html');
	}
	
	/**
	 * 修改分类
	 */
	public function editCate(){
		$cid=(int)$_GET['cid'];
		$sql="SELECT * FROM hd_category WHERE cid={$cid}";
		$editData=M()->query($sql);
		$this->assign('editData',$editData[0]);
		if(IS_POST){
			$title=$_POST['title'];
		    $sql="UPDATE  hd_category SET title='{$title}' WHERE cid={$cid}";
			M()->exec($sql);
			$this->success('修改成功',U('index'));
		}		
		$this->display('edit_cate.html');
	}
	
	/**
	 * 删除分类
	 */
	public function del(){
		$cid=intval($_GET['cid']);
		$sql="DELETE FROM hd_category WHERE cid={$cid}";
		M()->exec($sql);
		$this->success('删除成功',U('index'));
	}
	
 }
 

 
 
 
 
 
 







 ?>